package com.lk.pet.vet.controller;


import com.lk.pet.vet.model.Specialty;
import com.lk.pet.vet.model.Vet;
import com.lk.pet.vet.model.VetLog;
import com.lk.pet.vet.repos.VetLogRepository;
import com.lk.pet.vet.repos.VetRepository;
import com.lk.pet.vet.service.VetService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("/vet")
public class VetController {
    @Autowired
    VetService vetService;
    @Autowired
    VetRepository vetRepository;
    @Autowired
    VetLogRepository vetLogRepository;

    private static final Logger log = LoggerFactory.getLogger(VetController.class);

    @PostMapping("/")
    public Vet addVet(@RequestBody Vet vet){
        return vetService.add(vet);
    }

    @GetMapping("/{id}")
    public Optional<Vet> getVet(@PathVariable Integer id){
        log.debug("日志测试： I Got id = {}", id);
        log.info("日志测试： I Got id = " + id);
        return vetService.getVet(id);
    }

    @GetMapping("/vets")
    public List<Vet> vets(){
        return vetService.getVets();
    }

    @PostMapping("/specialty")
    public Specialty addSpecialty(String name){
        return vetService.add(name);
    }


    @GetMapping("/specialties")
    public List<Specialty> specialties(){
        return vetService.findAllSpecialties();
    }

    @PutMapping("/addSpecialtyToVet")
    public void addSpecialtyToVet(int vetId, int specialtyId){
        vetService.addSpecialtyToVet(vetId, specialtyId);
    }

    @PutMapping("/delSpecialtyFromVet")
    public void delSpecialtyFromVet(int vetId, int specialtyId){
        vetService.removeSpecialtyFromVet(vetId, specialtyId);
    }

    @DeleteMapping("/{id}")
    public void delVet(@PathVariable Integer id){
        vetRepository.deleteById(id);
    }

    @DeleteMapping("/specialty/{id}")
    public void delSpecialty(@PathVariable Integer id){
        vetService.delSpecialty(id);
    }

    @PostMapping("/log")
    public VetLog log(@RequestBody Map<String,String> body){
        return vetService.log( Integer.valueOf(body.get("vetId")), body.get("note"));
    }

    @DeleteMapping("/log/{id}")
    public void delLog(@PathVariable Integer id){
        vetLogRepository.deleteById(id);
    }

    @GetMapping("/logs")
    public List<VetLog> logs(){
        return vetLogRepository.findAll();
    }
    @GetMapping("/query")
    public List<Vet> query(String vetName){
        return vetRepository.findAllByNameLike("%"+vetName+"%");
    }

    @GetMapping("/log/query")
    public List<VetLog> queryVetLog(String vetName){
        return vetLogRepository.findAllByVetName(vetName);
    }

    @GetMapping("/log/query/page")
    public Page<VetLog> queryLogPageable(Pageable page){
        /*
          前端构造请求参数
          {
            "page": 0,
            "size": 2,
            "sort": ["id,desc"]

          }
         */
        return vetService.queryLog(page);
    }
}
